1 /** 2 Copyright: Copyright (c) 2018, Joakim Brännström. All rights reserved. 3 License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0) 4 Author: Joakim Brännström (joakim.brannstrom@gmx.com) 5 6 This is the main application file. 7 */ 8 module app; 9 10 import std.algorithm : among; 11 import std.exception : collectException; 12 import std.typecons : Flag, Yes, No; 13 import logger = std.experimental.logger; 14 15 import compile_db : CompileCommandDB; 16 import my.path : AbsolutePath, Path; 17 18 import code_checker.cli : Config; 19 20 int main(string[] args) { 21 import std.file : thisExePath, exists; 22 import std.functional : toDelegate; 23 import std.path : buildPath; 24 import code_checker.cli : AppMode, parseCLI, parseConfigCLI, loadConfig, Config; 25 import colorlog; 26 import app_normal; 27 28 auto conf = () { 29 Config conf; 30 try { 31 confLogger(VerboseMode.info); 32 auto miniConf = parseConfigCLI(args); 33 conf = Config.make(miniConf.workDir, miniConf.confFile); 34 if (exists(conf.baseUserConf)) { 35 loadConfig(conf, conf.baseUserConf); 36 } else { 37 logger.trace("No default configuration for code_checker found at: ", 38 conf.baseUserConf); 39 } 40 loadConfig(conf, miniConf.confFile); 41 } catch (Exception e) { 42 logger.warning(e.msg); 43 logger.error("Unable to read configuration: ", conf.confFile); 44 } 45 return conf; 46 }(); 47 parseCLI(args, conf); 48 confLogger(conf.logg.verbose); 49 logger.trace(conf); 50 51 alias Command = int delegate(Config conf); 52 Command[AppMode] cmds; 53 cmds[AppMode.none] = toDelegate(&modeNone); 54 cmds[AppMode.help] = toDelegate(&modeNone); 55 cmds[AppMode.helpUnknownCommand] = toDelegate(&modeNone_Error); 56 cmds[AppMode.normal] = toDelegate(&modeNormal); 57 cmds[AppMode.initConfig] = toDelegate(&modeInitConfig); 58 59 if (auto v = conf.mode in cmds) { 60 return (*v)(conf); 61 } 62 63 logger.error("Unknown mode %s", conf.mode); 64 return 1; 65 } 66 67 int modeNone(Config conf) { 68 return 0; 69 } 70 71 int modeNone_Error(Config conf) { 72 return 1; 73 } 74 75 int modeInitConfig(Config conf) { 76 import std.file : exists, copy; 77 import std.path : stripExtension; 78 79 if (exists(conf.confFile)) { 80 logger.error("Configuration file already exists: ", conf.confFile); 81 return 1; 82 } 83 84 const tmpl = conf.baseUserConf.stripExtension ~ "_template.toml"; 85 if (!exists(tmpl)) { 86 logger.error("Configuration template do not exist: ", tmpl); 87 return 1; 88 } 89 90 try { 91 copy(tmpl, cast(string) conf.confFile); 92 logger.info("Wrote configuration to ", conf.confFile); 93 } catch (Exception e) { 94 logger.error(e.msg); 95 return 1; 96 } 97 98 return 0; 99 }